﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using Newtonsoft.Json;
using System.Text;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Param
{
    public partial class NormalParamEdit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected SysParam _gourpDB;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _gourpDB.ItemValue = tb_value.Text;

                    session.Update(_gourpDB);
                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            // 刷新 editparamgroup 页面
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = GetKeepValue(_freshtabid)
            });
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
                tb_value.Text = _gourpDB.ItemValue;

                List<ParamItemExp> expItemList;

                if (_gourpDB.Param5 != null)
                {
                    //可能没有配置扩展
                    expItemList = JsonConvert.DeserializeObject<List<ParamItemExp>>(_gourpDB.Param5.ToString());

                    if (expItemList != null)
                    {
                        foreach (ParamItemExp itemExp in expItemList)
                        {
                            // 固定为 Name 的项
                            if (itemExp.Code == "Name")
                            {
                                tb_name.Text = itemExp.Value;
                            }
                        }
                    }
                }
            }
        }

        protected void InitializeGlobalValue()
        {
            KeepValue("uid", _uid);
            KeepValue("gid", _gid);
            KeepValue("freshtabid", _freshtabid);

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _gourpDB = session.CreateCriteria<SysParam>().Add(Expression.Eq("GroupCode", GetKeepValue(_gid))).Add(Expression.Eq("ItemCode", GetKeepValue(_uid))).UniqueResult<SysParam>();

                    if (_gourpDB == null)
                    {
                        throw new Exception("找不到对应参数信息.");
                    }
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
        }

        protected void KeepValue(string UrlParam, Label KeepLabel)
        {
            try
            {
                // 提交时将没有URL中的参数，因此使用隐藏控件保存
                if (!IsPostBack)
                {
                    KeepLabel.Text = Request[UrlParam] == null ? "" : Request[UrlParam];
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected string GetKeepValue(Label KeepLabel)
        {
            return KeepLabel.Text;
        }
    }
}
